// pages/express/list/list.js
Page({
  data: {
    expressList: [],
    loading: false,
    hasMore: true,
    page: 1,
    pageSize: 10,
    filterType: 'all',
    searchKeyword: ''
  },

  onLoad() {
    this.loadExpressList()
  },

  onPullDownRefresh() {
    this.setData({
      page: 1,
      expressList: [],
      hasMore: true
    })
    this.loadExpressList()
    wx.stopPullDownRefresh()
  },

  onReachBottom() {
    if (this.data.hasMore && !this.data.loading) {
      this.loadExpressList()
    }
  },

  loadExpressList() {
    if (this.data.loading) return
    
    this.setData({ loading: true })
    
    // 模拟API调用
    setTimeout(() => {
      const mockData = this.generateMockData()
      this.setData({
        expressList: this.data.page === 1 ? mockData : [...this.data.expressList, ...mockData],
        loading: false,
        hasMore: mockData.length === this.data.pageSize,
        page: this.data.page + 1
      })
    }, 1000)
  },

  generateMockData() {
    const companies = ['中通快递', '圆通速递', '申通快递', '韵达快递', '顺丰速运', '京东快递']
    const pickupLocations = ['菜鸟驿站(东门)', '快递柜A区', '菜鸟驿站(南门)', '快递柜B区']
    const deliveryLocations = ['宿舍楼1号', '宿舍楼2号', '宿舍楼3号', '教学楼', '图书馆']
    
    return Array.from({ length: this.data.pageSize }, (_, i) => ({
      id: Date.now() + i,
      company: companies[Math.floor(Math.random() * companies.length)],
      pickupLocation: pickupLocations[Math.floor(Math.random() * pickupLocations.length)],
      deliveryLocation: deliveryLocations[Math.floor(Math.random() * deliveryLocations.length)],
      pickupCode: Math.random().toString().substr(2, 4),
      reward: Math.floor(Math.random() * 10) + 5,
      urgent: Math.random() > 0.8,
      requiredTime: '今天18:00前',
      description: '帮忙代拿快递，送到宿舍楼下即可，谢谢！',
      publisher: '同学' + Math.floor(Math.random() * 1000),
      publishTime: Math.floor(Math.random() * 5) + 1 + '小时前',
      distance: (Math.random() * 2).toFixed(1),
      status: 'pending'
    }))
  },

  onFilterChange(e) {
    const filterType = e.currentTarget.dataset.type
    this.setData({
      filterType,
      page: 1,
      expressList: [],
      hasMore: true
    })
    this.loadExpressList()
  },

  onSearchInput(e) {
    this.setData({
      searchKeyword: e.detail.value
    })
  },

  onSearch() {
    this.setData({
      page: 1,
      expressList: [],
      hasMore: true
    })
    this.loadExpressList()
  },

  navigateToDetail(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/express/detail/detail?id=${id}`
    })
  },

  navigateToPublish() {
    wx.navigateTo({
      url: '/pages/express/publish/publish'
    })
  }
})